<?php

	/*
	 * To change this license header, choose License Headers in Project Properties.
	 * To change this template file, choose Tools | Templates
	 * and open the template in the editor.
	 */

	namespace Phptricks\app\controllers\users;

	/**
	 * Description of groups
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\app\models\groups\groups as groups_model;
	use Phptricks\app\models\users\users as users_model;
	use Phptricks\app\models\countrys\countrys as countrys_model;
	use Phptricks\app\models\trieds\trieds as trieds_model;

	class login extends base{

		
		public function __construct(){
			parent::__construct();
			$this->user_fields = array(
				'user_id',
				'user_group_id',
				'user_country_id',
				'user_name',
				'user_fullname',
				'user_password',
				'user_email',
				'user_about',
				'user_image',
				'user_stop',
				'user_date_registering',
				'user_fields',
				'count_tried',
				'count_comment',
				'reputation'
			);
			$this->groups_model = groups_model::getinstance();
			$this->users_model = users_model::getinstance();
			$this->trieds_model = trieds_model::getinstance();

			$groups = $this->groups_model->get_groups();
			$this->view->assign('groups', $groups);

			$this->view->assign('page_title', $this->lang['users_controll']);
		}

		public function admin_index($group_id = 0, $order_by = 'user_date_registering', $sort_type = 'DESC'){
			$group_id = \intval($group_id);
			if (\in_array($order_by, $this->user_fields)) {
				if (\strtoupper($sort_type) == 'ASC') {
					$sort_type = 'ASC';
				} else {
					$sort_type = 'DESC';
				}
				if (!empty($order_by)) {
					$order_by = $order_by . " " . $sort_type;
				} else {
					$order_by = '';
				}
			} else {
				$order_by = '';
			}

			if ($group_id > 0) {
				$users = $this->users_model->get_users($group_id, true, $order_by);
			} else {
				$users = $this->users_model->get_users('', false, $order_by);
			}


			if (\strtoupper($sort_type) == 'ASC') {
				$sort_type = 'DESC';
			} else {
				$sort_type = 'ASC';
			}
			$this->view->assign('sort_type', $sort_type);
			$this->view->assign('group_id', $group_id);
			$this->view->assign('users', $users);
			$this->view->display('users.tpl');
		}

		public function admin_view($group_id = 0, $order_by = 'user_date_registering', $sort_type = 'DESC'){
			$this->admin_index($group_id, $order_by, $sort_type);
		}

		public function admin_search($sort_type = 'DESC'){
			if (\strtoupper($sort_type) == 'ASC') {
				$sort_type = 'DESC';
			} else {
				$sort_type = 'ASC';
			}
			$this->view->assign('sort_type', $sort_type);
			$this->view->assign('group_id', 0);
			$this->form_validation->post(array(
				'sraech_data' => 'isnull|minlength:2'
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/users/view/', 5);
			}
			$search_data = $this->form_validation->_array_data['sraech_data'];
			$where_array = array(
				'user_name|LIKE' => $search_data,
				'user_fullname|LIKE' => $search_data,
				'user_email|LIKE' => $search_data
			);
			$users = $this->users_model->get_users_by_data($where_array, false, 'user_date_registering ' . $sort_type);
			$this->view->assign('users', $users);
			$this->view->display('users.tpl');
		}

		public function admin_inactive($group_id = 0, $order_by = 'user_date_registering', $sort_type = 'DESC'){
			$group_id = \intval($group_id);
			if (\in_array($order_by, $this->user_fields)) {
				if (\strtoupper($sort_type) == 'ASC') {
					$sort_type = 'ASC';
				} else {
					$sort_type = 'DESC';
				}
				if (!empty($order_by)) {
					$order_by = $order_by . " " . $sort_type;
				} else {
					$order_by = '';
				}
			} else {
				$order_by = '';
			}
			/**
			 *
			 * user_stop data type:
			 *  0 = stop
			 *  1 = inactive
			 *  2 = active
			 *
			 */
			$array_where = array(
				'user_stop' => '2',
			);

			if ($group_id > 0) {
				$users = $this->users_model->get_users_by_data($array_where, $group_id, true, $order_by);
			} else {
				$users = $this->users_model->get_users_by_data($array_where, '', false, $order_by);
			}


			if (\strtoupper($sort_type) == 'ASC') {
				$sort_type = 'DESC';
			} else {
				$sort_type = 'ASC';
			}
			$this->view->assign('sort_type', $sort_type);
			$this->view->assign('group_id', $group_id);
			$this->view->assign('users', $users);
			$this->view->display('users.tpl');
		}

		public function admin_edit($id){
			$id = \intval($id);
			if (empty($id)) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/users/view/', 5);
			}
			$this->countrys_model = countrys_model::getinstance();
			$countrys = $this->countrys_model->getcountrys('', 'country_name ASC');
			$this->view->assign('countrys', $countrys);
			$data = $this->users_model->get_users($id);
			if (\count($data) > 0) {
				$data = $data[0];
				if (!empty($data['user_fields'])) {
					$data['user_fields'] = $this->custom_fields->decode($data['user_fields']);
				}
			}
			$array_where = array(
				'tried_user_id' => $id,
			);
			$user_trieds = $this->trieds_model->get_trieds($array_where, 'tried_id DESC', 'tried_id',7);
			$this->view->assign('user_trieds', $user_trieds);
			$this->view->assign('data', $data);
			$this->view->display('users.tpl');
		}

		public function admin_update(){
			$this->form_validation->post(array(
				'user_id' => 'isnull|digit',
				'user_group_id' => 'isnull|digit',
				'user_country_id' => 'isnull|digit',
				'user_fullname' => 'isnull',
				'user_name' => 'isnull|maxlength:' . $this->settings['users_max'] . '|minlength:' . $this->settings['users_min'] . '|',
				'user_password' => 'isnull|minlength:' . $this->settings['pass_min'] . '|maxlength:' . $this->settings['pass_max'],
				'user_email' => 'isnull|checkmail',
				'user_about' => '',
				'user_image' => '',
				'user_stop' => 'isnull|digit',
			));

			if ($this->form_validation->_array_data['user_password'] != '123456789') {
				$this->form_validation->_array_data['user_password'] = md5($this->form_validation->_array_data['user_password']);
			} else {
				$this->form_validation->unset_var('user_password', 'post');
			}

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$url = 'admin/users/edit/' . $this->form_validation->_array_data['user_id'];
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), $url, 5);
			}
			$user_id = $this->form_validation->_array_data['user_id'];
			$array_where = array(
				'user_id' => $user_id
			);
			$this->form_validation->unset_var('user_id', 'post');
			$this->users_model->updatedata($this->form_validation->_array_data, $array_where);
			$this->display_message('success', $this->lang['success_edit_data'], 'admin/users/edit/' . $user_id, 3);
		}

		public function admin_add(){
			$this->countrys_model = countrys_model::getinstance();
			$countrys = $this->countrys_model->getcountrys('', 'country_name ASC');
			$this->view->assign('countrys', $countrys);
			$data = array();
			$data['group_options'] = $this->settings['default_group_settings'];
			$this->view->assign('data', $data);
			$this->view->display('users.tpl');
		}

		public function admin_addnew(){
			$this->form_validation->post(array(
				'user_group_id' => 'isnull|digit',
				'user_country_id' => 'isnull|digit',
				'user_fullname' => 'isnull',
				'user_name' => 'isnull|maxlength:' . $this->settings['users_max'] . '|minlength:' . $this->settings['users_min'] . '|',
				'user_password' => 'isnull|minlength:' . $this->settings['pass_min'] . '|maxlength:' . $this->settings['pass_max'] . '|md5val',
				'user_email' => 'isnull|checkmail',
				'user_about' => '',
				'user_image' => '',
				'user_stop' => 'isnull|digit',
				'user_date_registering' => $this->datetime->_full_date,
			));

			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/users/view/', 5);
			}
			$this->users_model->adduser($this->form_validation->_array_data);
			$this->display_message('success', $this->lang['success_add_data'], 'admin/users/view/', 3);
		}

		public function admin_delete($id){
			if (!empty($id) && isset($id)) {
				if (\is_array($id)) {
					$id = \array_filter($id, function(&$id_val) {
						echo $id_val . "<br>";
						return \intval($id_val);
					});
				} else {
					$id = \intval($id);
				}
				$where_array = array(
					'group_id' => $id
				);
				$this->groups_model->deletegroups($where_array);
				$this->display_message('success', $this->lang['success_delete_data'], 'admin/users/view/', 3);
			}
		}

		public function admin_action(){
			if ($this->form_validation->is_set('row_id') == false || $this->form_validation->is_set('action_type') == false) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/users/view/', 5);
			}
			$id = $this->form_validation->_post['row_id'];
			if (\is_array($id)) {
				$id = \array_filter($id, function(&$id_val) {
					return \intval($id_val);
				});
			} else {
				$id = \intval($id);
			}
			switch ($this->form_validation->_post['action_type']) {
				case 'delete':
					if (!empty($id)) {
						$where_array = array(
							'group_id' => $id
						);
						try {
							$this->groups_model->deletegroups($where_array);
						} catch (\Exception $exc) {
							throw new Exc($exc->getMessage());
						}
					}
					break;

				case 'active_users':
				case 'stop_account':
					if (!empty($id)) {
						$where_array = array(
							'user_id' => $id
						);
						$user_stop = '0';
						$msg = $this->lang['success_stop_users'];
						if ($this->form_validation->_post['action_type'] == 'active_users') {
							$user_stop = '2';
							$msg = $this->lang['success_active_users'];
						}
						$updata_array = array(
							'user_stop' => $user_stop
						);
						try {
							$this->users_model->updatedata($updata_array, $where_array);
							$this->display_message('success', $msg, 'admin/users/view/', 3);
						} catch (\Exception $exc) {
							throw new Exc($exc->getMessage());
						}
					}
					break;
				case 'send_active':
					if (!empty($id)) {
						$where_array = array(
							'user_id' => $id
						);

						$updata_array = array(
							'user_stop' => '0'
						);
						try {
							$this->users_model->updatedata($updata_array, $where_array);
							$this->display_message('success', $this->lang['success_active_users'], 'admin/users/view/', 3);
						} catch (\Exception $exc) {
							throw new Exc($exc->getMessage());
						}
					}
					break;

				default:
					if (\intval($this->form_validation->_post['action_type']) > 0) {
						if (!empty($id)) {
							$where_array = array(
								'user_group_id' => $id
							);
							$updata_array = array(
								'user_group_id' => \intval($this->form_validation->_post['action_type'])
							);
							$this->users_model->updatedata($updata_array, $where_array);
							$this->display_message('success', $this->lang['success_move_user'], 'admin/users/view/', 3);
						}
					} else {
						$this->display_message('danger', $this->lang['error_select_data'], 'admin/users/view/', 5);
					}
					break;
			}
		}

	}
	